#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QToolButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include "SessionListWidget.h"

ListWidget::ListWidget() {
	openSessionButton = new QToolButton;
	openSessionButton->setIcon(QIcon(":/icons/open.png"));
	openSessionButton->setAutoRaise(true);
	openSessionButton->setToolTip(tr("Open session"));
	
	newSessionButton = new QToolButton;
	newSessionButton->setIcon(QIcon(":/icons/new.png"));
	newSessionButton->setAutoRaise(true);
	newSessionButton->setToolTip(tr("Start new session"));
	
	saveAsSessionButton = new QToolButton;
	saveAsSessionButton->setIcon(QIcon(":/icons/saveas.png"));
	saveAsSessionButton->setAutoRaise(true);
	saveAsSessionButton->setToolTip(tr("Save current session as"));
	saveAsSessionButton->setShortcut(QKeySequence("Ctrl+S"));
	
	removeSessionButton = new QToolButton;
	removeSessionButton->setIcon(QIcon(":/icons/remove.png"));
	removeSessionButton->setAutoRaise(true);
	removeSessionButton->setToolTip(tr("Remove session"));
	removeSessionButton->setShortcut(QKeySequence("Delete"));
	
	lineEdit = new QLineEdit;
	
	defaultPalette = lineEdit->palette();
	redPalette.setColor(QPalette::Base,QColor(255,110,110));
	
	closeButton = new QToolButton;
	closeButton->setIcon(QIcon(":/icons/close.png"));
	closeButton->setAutoRaise(true);
	closeButton->setShortcut(QKeySequence("Esc"));
	
	saveButton = new QToolButton;
	saveButton->setIcon(QIcon(":/icons/save.png"));
	saveButton->setAutoRaise(true);
	saveButton->setEnabled(false);
	saveButton->setShortcut(QKeySequence("Enter"));
	
	QHBoxLayout *bottomLayout = new QHBoxLayout;
	bottomLayout->addStretch();
	bottomLayout->addWidget(newSessionButton);
	bottomLayout->addWidget(openSessionButton);
	bottomLayout->addWidget(saveAsSessionButton);
	bottomLayout->addStretch();
	bottomLayout->addWidget(removeSessionButton);
	
	QHBoxLayout *lineEditLayout = new QHBoxLayout;
	lineEditLayout->addWidget(new QLabel(tr("Name") + ":"));
	lineEditLayout->addWidget(lineEdit,1);
	lineEditLayout->addWidget(saveButton);
	lineEditLayout->addWidget(closeButton);
	lineEditLayout->setContentsMargins(0,0,0,0);
	
	inputLine = new QWidget;
	inputLine->setLayout(lineEditLayout);
	inputLine->hide();
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addStretch();
	mainLayout->addWidget(inputLine);
	mainLayout->addLayout(bottomLayout);
	setLayout(mainLayout);
	
	connect(saveAsSessionButton,SIGNAL(clicked()),this,SLOT(showInputLine()));
	connect(closeButton,SIGNAL(clicked()),inputLine,SLOT(hide()));
	connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(textChanged(const QString&)));
	connect(newSessionButton,SIGNAL(clicked()),this,SIGNAL(startNewSession()));
	connect(saveButton,SIGNAL(clicked()),this,SLOT(emitSignalToSaveSessionAs()));
	connect(openSessionButton,SIGNAL(clicked()),this,SLOT(emitSignalToOpenSession()));
	connect(lineEdit,SIGNAL(returnPressed()),this,SLOT(emitSignalToSaveSessionAs()));
	connect(removeSessionButton,SIGNAL(clicked()),this,SLOT(emitSignalToRemoveSession()));
	connect(this,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(emitSignalToOpenSession()));
}

ListWidget::~ListWidget() {
	delete openSessionButton;
	delete newSessionButton;
	delete saveAsSessionButton;
	delete removeSessionButton;
	delete closeButton;
	delete saveButton;
	delete lineEdit;
	delete inputLine;
}

void ListWidget::textChanged(const QString& text) {
	bool isEnabled = true;
	if (text.isEmpty()) {
		isEnabled = false;
		lineEdit->setPalette(defaultPalette);
	} else {
		for (int i = 0; i < count(); i++)
			if (item(i)->text() == text.trimmed() || text == "Sessions") {
				isEnabled = false;
				lineEdit->setPalette(redPalette);
				break;
			} else
				lineEdit->setPalette(defaultPalette);
	}
	saveButton->setEnabled(isEnabled);
}

void ListWidget::showInputLine() {
	inputLine->show();
	lineEdit->setFocus();
}

void ListWidget::emitSignalToSaveSessionAs() {
	if (saveButton->isEnabled()) {
		emit(saveSessionAs(lineEdit->text()));
		lineEdit->clear();
		inputLine->hide();
	}
}

void ListWidget::emitSignalToOpenSession() {
	if (currentItem() != 0)
		emit (openSession(currentItem()->text()));
}

void ListWidget::emitSignalToRemoveSession() {
	if (currentItem() != 0)
		emit (removeSession(currentItem()->text()));
}
